package com.LostAndFound.service;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.LostAndFound.dao.UserHomeDao;
import com.LostAndFound.utils.DataSourceUtils;

public class UserHomeService {
	UserHomeDao dao = new UserHomeDao();

	public List<Map<String, Object>> getAllSearchLoseRecords(String whatFlag) {
		List<Map<String, Object>> articleList = null;
		try {
			articleList = dao.getAllSearchLoseRecords(whatFlag);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return articleList;
	}

	public Map<String, Object> showArticleDetailByAid(String aid) {
		Map<String, Object> article = null;
		try {
			article = dao.showArticleDetailByAid(aid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return article;
	}

	public List<Map<String, Object>> getMySearchRecord(String whatFlag, String searchContent) {
		List<Map<String, Object>> articleList = null;
		try {
			articleList = dao.getMySearchRecord(whatFlag,searchContent);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return articleList;
	}

	// 确定自己捡到失物，或者自己就是失主。修改foundFlag字段，重新读取article写入session
	public Map<String, Object> handleForConfirmTakenArticle(String aid) {
		Map<String, Object> article = null;
		try {
			DataSourceUtils.startTransaction();
			// 先修改
			dao.handleUpdateArticleForConfirmTaken(aid);
			// 在读取，写入session
			article = dao.showArticleDetailByAid(aid);
		} catch (SQLException e) {
			try {
				DataSourceUtils.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				DataSourceUtils.commitAndRelease();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return article;
	}

}
